﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SEO.Business;

/// <summary>
/// 关键字报表
/// </summary>
public partial class KeywordsReport : AdminPage
{
    /// <summary>
    /// 时间
    /// </summary>
    protected string BeginDateTime
    {
        get { return ViewState["BeginDateTime"] == null ? DateTime.Now.ToString("yyyy-MM-dd") : ViewState["BeginDateTime"].ToString(); }
        set { ViewState["BeginDateTime"] = value; }
    }

    /// <summary>
    /// 分类ID
    /// </summary>
    protected int SortID
    {
        get { return ViewState["SortID"] == null ? 0 : Convert.ToInt32(ViewState["SortID"]); }
        set { ViewState["SortID"] = value; }
    }

    /// <summary>
    /// 类型
    /// </summary>
    protected int CountType
    {
        get { return ViewState["CountType"] == null ? 1 : Convert.ToInt32(ViewState["CountType"]); }
        set { ViewState["CountType"] = value; }
    }

    protected int nCount = 0;
    protected int nNum = 0;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitSort();
            InitData();
        }
    }

    protected void InitSort()
    {
        DataTable dtSort = GetAllSort();
        ddlSort.Items.Clear();
        ddlSort.Items.Add(new ListItem("所有分类", "0"));
        foreach (DataRow drSort in dtSort.Rows)
        {
            ddlSort.Items.Add(new ListItem(drSort["Name"].ToString(), drSort["ID"].ToString()));
        }
        dtSort.Dispose();
        txtDateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
    }

    /// <summary>
    /// 页面初始化
    /// </summary>
    protected void InitData()
    {
        DataTable dtMedical = new BisMedical().GetList();
        rptMedical.DataSource = dtMedical;
        rptMedical.DataBind();
        nCount = dtMedical.Rows.Count;

        int nRecordCount = 0;
        int nPageCount = 0;

        if (string.IsNullOrEmpty(BeginDateTime))
        {
            BeginDateTime = DateTime.Now.ToString("yyyy-MM-dd");
        }
        DateTime dBeginTime = Convert.ToDateTime(BeginDateTime + " 00:00:00");
        //信息列表绑定
        BisKeywords bisKeywords = new BisKeywords();
        DataTable dt = bisKeywords.GetList("", SortID,-1,-1,anpPage.CurrentPageIndex, anpPage.PageSize, ref nPageCount, ref nRecordCount);
        nNum = (anpPage.CurrentPageIndex - 1) * anpPage.PageSize;
        nNum++;
        for (int i = 1; i <= nCount; i++)
        {
            dt.Columns.Add("Item" + i.ToString(), typeof(int));
        }
        string strIDList = string.Empty;
        foreach (DataRow dr in dt.Rows)
        {
            strIDList += (string.IsNullOrEmpty(strIDList) ? "" : ",") + dr["ID"];
        }
        if (!string.IsNullOrEmpty(strIDList))
        {
            //查询关键字报表
            DataTable dtReport =new BisKeywordsReport().GetReportList(strIDList, dBeginTime, dBeginTime.AddDays(1));
            if (dtReport != null && dtReport.Rows.Count > 0)
            {
                foreach (DataRow drKey in dt.Rows)
                {
                    for (int j = 0; j < dtMedical.Rows.Count; j++)
                    {
                        DataRow[] drReportList = dtReport.Select("KeywordsID=" + drKey["ID"].ToString() + " and MedicalID=" + dtMedical.Rows[j]["ID"].ToString());
                        if (drReportList.Length > 0)
                        {
                            switch (CountType)
                            {
                                case 0:
                                    drKey["Item" + (j + 1).ToString()] = Convert.ToInt32(drReportList[0]["FirstCount"]) + Convert.ToInt32(drReportList[0]["SecondCount"]) + Convert.ToInt32(drReportList[0]["ThirdCount"]);
                                    break;
                                case 1:
                                    drKey["Item" + (j + 1).ToString()] = Convert.ToInt32(drReportList[0]["FirstCount"]);
                                    break;
                                case 2:
                                    drKey["Item" + (j + 1).ToString()] = Convert.ToInt32(drReportList[0]["SecondCount"]);
                                    break;
                                case 3:
                                    drKey["Item" + (j + 1).ToString()] = Convert.ToInt32(drReportList[0]["ThirdCount"]);
                                    break;
                            }
                            
                        }
                    }
                }
            }
            dtReport.Dispose();
        }
        anpPage.RecordCount = nRecordCount;
        if (nRecordCount == 0)
        {
            noData.Style["display"] = "block";
        }
        else
        {
            noData.Style["display"] = "none";
        }
        rptKeywords.DataSource = dt;
        rptKeywords.DataBind();
        dt.Dispose();
        dtMedical.Dispose();

        anpPage.CustomInfoHTML = string.Format("<div class=\"pagetag\">当前第<b>{0}</b>/<b>{1}</b>页 共<b>{2}</b>条记录</div>", new object[] { anpPage.CurrentPageIndex, anpPage.PageCount, anpPage.RecordCount });
    }

    /// <summary>
    /// 刷新页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click(object sender, EventArgs e)
    {
        InitData();
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       
        SortID = Convert.ToInt32(ddlSort.SelectedValue);
        BeginDateTime = txtDateTime.Text;
        CountType = Convert.ToInt32(ddlType.SelectedValue);
        anpPage.CurrentPageIndex = 1;
       // InitData();
    }

    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="src"></param>
    /// <param name="e"></param>
    protected void anpPage_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        anpPage.CurrentPageIndex = e.NewPageIndex;
        InitData();
    }

    /// <summary>
    /// 列表绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptKeywords_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataTable dt = (DataTable)((Repeater)sender).DataSource;
        Literal ltlTD = e.Item.FindControl("ltlTD") as Literal;
        for (int i = 1; i <= nCount; i++)
        {
            ltlTD.Text += "<td>" + (dt.Rows[e.Item.ItemIndex]["Item" + i.ToString()].ToString() == string.Empty ? "0" : dt.Rows[e.Item.ItemIndex]["Item" + i.ToString()].ToString()) + "</td>";
        }
    }

    /// <summary>
    /// 生成Excel
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "window.open('CreateExcel.aspx?type=keywordsreport&datetime=" + BeginDateTime + "&counttype=" + CountType + "');", true);
    }
}